// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: MIT

import { HorizontalBox, VerticalBox, StandardTableView, GroupBox, StyleMetrics, LineEdit } from "std-widgets.slint";
import { GallerySettings } from "../gallery_settings.slint";
import { Page } from "page.slint";

export global TableViewPageAdapter  {
    in property <[[StandardListViewItem]]> row_data: [
        [ { text: "Item 1.1" }, { text: "Item 1.2" }, { text: "Item 1.3" }, { text: "Item 1.4" }, ],
        [ { text: "Item 2.1" }, { text: "Item 2.2" }, { text: "Item 2.3" }, { text: "Item 2.4" }, ],
        [ { text: "Item 3.1" }, { text: "Item 3.2" }, { text: "Item 3.3" }, { text: "Item 3.4" }, ],
        [ { text: "Item 4.1" }, { text: "Item 4.2" }, { text: "Item 4.3" }, { text: "Item 4.4" }, ],
        [ { text: "Item 5.1" }, { text: "Item 5.2" }, { text: "Item 5.3" }, { text: "Item 5.4" }, ],
        [ { text: "Item 6.1" }, { text: "Item 6.2" }, { text: "Item 6.3" }, { text: "Item 6.4" }, ],
    ];

    pure callback filter_sort_model([[StandardListViewItem]], string, int, bool) -> [[StandardListViewItem]];
    filter_sort_model(row-data, filter, sort-index, sort-ascending) => { return row-data; }
}

export component TableViewPage inherits Page {
    property <int> sort-index: -1;
    property <bool> sort-ascending;

    title: @tr("TableView");
    show-enable-switch: false;
    description: @tr("StandardTableView can be used to display a list of text elements in columns and rows. It can be imported from \"std-widgets.slint\"");

    HorizontalBox {
        vertical-stretch: 1;

        GroupBox {
            title: @tr("StandardTableView");
            vertical-stretch: 0;

            VerticalLayout {
                spacing: StyleMetrics.layout-spacing;

                StandardTableView {
                    sort-ascending(index) => {
                        root.sort-index = index;
                        root.sort-ascending = true;
                    }

                    sort-descending(index) => {
                        root.sort-index = index;
                        root.sort-ascending = false;
                    }

                    columns: [
                        { title: @tr("Header 1") },
                        { title: @tr("Header 2") },
                        { title: @tr("Header 3") },
                        { title: @tr("Header 4") },
                    ];
                    rows: TableViewPageAdapter.filter_sort_model(TableViewPageAdapter.row_data, filter-edit.text, root.sort-index, root.sort-ascending);
                }

                VerticalLayout {
                    Text {
                        text: @tr("Filter by Header 1:");
                    }

                    filter-edit := LineEdit {
                        placeholder-text: @tr("Enter filter text");
                    }
                }
            }
       }
    }
}
